package com.example.myapplication.ui;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.fragment.BaseFragment;
import com.example.myapplication.fragment.ListViewFragment;
import com.example.myapplication.R;
import com.example.myapplication.fragment.MainFragment;

public class PullToRefreshActivity extends AppCompatActivity {

    private List<BaseFragment> mFragmentsList;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pull_to_refresh);

        initFragmentList();
        //初始化ViewPager
        initViewsPager();
    }

    private void initFragmentList() {
        mFragmentsList = new ArrayList<>();
        mFragmentsList.add(new MainFragment());
    }

    private void initViewsPager() {
        mViewPager = findViewById(R.id.pager);
        FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return mFragmentsList.size();
            }

            @NonNull
            @Override
            public Fragment getItem(int position) {
//                return mFragmentsList.get(position);
                return new ListViewFragment();
            }
        };

        //设置ViewPager的适配器
        mViewPager.setAdapter(fragmentPagerAdapter);
        //设置viewPager的页面切换
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}
